// pages/search_main/search_main.js
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        focus: false,
        school_modal: '',
        select_school_id: '',
        select_school_name: '',

        role_modal: '',
        select_role: '',

        school_id: '',
        school_name: '',

        course: [],
        cate_id: '',
        keyword: '',
        cate: [],
        page: 1,
        is_empty: false,
        
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.cate_id != undefined) {
            this.setData({
                cate_id: options.cate_id
            })
        }
        this.getUserInfo();
    },

    onLogin: function (options) {
        this.getUserInfo();
    },

    //请先登录
    getUserInfo: function () {
        let _this = this;
        app.beLogin(function () {
            app._get('/api/login/user/getUserInfo', {
            }, function (response) {
                let data = response.data.data;
                if (response.data.code == 200) {
                    _this.setData({
                        user: data,
                    })
                    app.globalData.user = data;
                    //如果登录后没有绑定学校
                    if(data.school_id==null||data.school_name==null)
                    {
                        _this.showSchool();
                    }
                    //如果登录后没有绑定角色
                    else if(data.role==null)
                    {
                       _this.showRole();
                    }else{
                        _this.setData({
                            school_id: data.school_id,
                            school_name: data.school_name
                        })
                        _this.getCate();
                        _this.renderCourseList();
                    }
                }
            })
        });
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh: function () {
        wx.showLoading({
            title: '正在刷新',
        })
        this.renderCourseList(function () {
            setTimeout(function () {
                wx.stopPullDownRefresh();
                wx.hideLoading()
            }, 500)
        })
    },

    /**
     * 上拉分页加载
     */
    onReachBottom: function () {
        this.getCourseList();
    },

    //搜索
    search: function (input) {
        let keyword = input.detail.value,
            setdata = {};
        setdata['keyword'] = keyword;
        setdata['page'] = 1;
        setdata['course'] = [];
        this.setData(setdata);
        this.getCourseList();

    },

    //分类
    change_tab: function (e) {
        let setdata = {};
        setdata['cate_id'] = e.currentTarget.dataset.cate_id
        setdata['page'] = 1;
        setdata['course'] = [];
        this.setData(setdata);
        this.getCourseList();
    },

    //重新渲染商品数据
    renderCourseList: function (cb) {
        let setdata = {};
        setdata['course'] = [];
        setdata['page'] = 1;
        this.setData(setdata)
        this.getCourseList(cb);
    },

    //获取课程列表
    getCourseList: function (cb) {
        let _this = this,
        cate_id = _this.data.cate_id,
        keyword = _this.data.keyword,
        page = _this.data.page,
        origin = _this.data.course;
        app._get('/api/login/course/getAllCourse', {
            page: page,
            cate_id: cate_id,
            keyword: keyword
        }, function (response) {
            let data = response.data.data;
            data = origin.length > 0 ? origin.concat(data) : data;
            let setdata = {};
            if (data.length == 0) {
                setdata['is_empty'] = true;
            } else {
                setdata['is_empty'] = false;
                setdata['course'] = data;
                setdata['page'] = page + 1;
            }
            _this.setData(setdata);
        })
        typeof cb == 'function' && cb();
    },

    //获取分类
    getCate: function (cb) {
        let _this = this;
        app._get('/api/login/course/getAllCate', null, function (response) {
            _this.setData({
                cate: response.data.data
            })
        })
    },

    //显示弹窗
    showSchool: function () {
        this.getSchool();
        this.setData({
            school_modal: 'show'
        })
    },

    //选择学校
    selectSchool(e){
        this.setData({
            select_school_id: e.currentTarget.dataset.school_id,
            select_school_name: e.currentTarget.dataset.school_name
        })
    },

     //获取学校列表
     getSchool: function () {
        let _this = this;
        app._get('/api/school/getSchool', null, function (response) {
            _this.setData({
                school: response.data.data
            })
        })
    },
    

    //确定学校后
    confirmSchool: function () {
        let _this = this;
        app._post('/api/login/user/updateSchool', {
            school_id: _this.data.select_school_id,
            school_name: _this.data.select_school_name
        }, function (response) {
            if (response.data.code == 200) {
                _this.setData({
                    school_modal: '',
                    school_id: _this.data.select_school_id,
                    school_name: _this.data.select_school_name,
                })
                app.globalData.school_id=_this.data.select_school_id;
                app.globalData.school_name=_this.data.select_school_name;
                _this.getUserInfo();
            }
        })    
    },

    //显示弹窗
    showRole: function () {
        this.getSchool();
        this.setData({
            role_modal: 'show'
        }) 
    },

    //选择学生
    checkStudent(){
        this.setData({
            select_role: 1
        })
    },

    //选择教师
    checkTeacher(){
        this.setData({
            select_role: 2
        })
    },

     //选择班主任
     checkClassmaster(){
        this.setData({
            select_role: 5
        })
    },
    
     //选择校长
     checkSchoolmaster(){
        this.setData({
            select_role: 6
        })
    },
    
    
    //确定角色后
    confirmRole(){
        this.setData({
            role: this.data.select_role
        })

        let _this = this;
        app._post('/api/login/user/updateRole', {
            role: _this.data.select_role
        }, function (response) {
            console.log(response)
            if (response.data.code == 200) {
                _this.setData({
                    role_modal: '',
                    role: _this.data.select_role
                })
                app.globalData.role=_this.data.select_role
                _this.getUserInfo();
            }
        })
    },


    /**
     * 去课程详情
     */
    toDetail: function(e){
        wx.navigateTo({
          url: '/pages/course_detail/course_detail?course_id='+e.currentTarget.dataset.course_id,
        })
    },

})